package com.ruoyi.labdanger.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.labdanger.domain.LabApplyPurchase;
import com.ruoyi.labdanger.service.ILabApplyPurchaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请采购Controller
 * 
 * @author winner pan
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/labdanger/purchase")
public class LabApplyPurchaseController extends BaseController {
	@Autowired
	private ILabApplyPurchaseService labApplyPurchaseService;

	/**
	 * 查询申请采购列表
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:query')")
	@GetMapping("/list")
	public TableDataInfo list(LabApplyPurchase labApplyPurchase) {
		startPage();
		List<LabApplyPurchase> list = labApplyPurchaseService.selectLabApplyPurchaseList(labApplyPurchase);
		return getDataTable(list);
	}

	/**
	 * 导出申请采购列表
	 */
	/*@PreAuthorize("@ss.hasPermi('labdanger:purchase:export')")
	@Log(title = "申请采购", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(LabApplyPurchase labApplyPurchase) {
		List<LabApplyPurchase> list = labApplyPurchaseService.selectLabApplyPurchaseList(labApplyPurchase);
		ExcelUtil<LabApplyPurchase> util = new ExcelUtil<LabApplyPurchase>(LabApplyPurchase.class);
		return util.exportExcel(list, "purchase");
	}*/

	/**
	 * 获取申请采购详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:query')")
	@GetMapping(value = "/{instanceId}")
	public AjaxResult getInfo(@PathVariable("instanceId") Long instanceId) {
		return AjaxResult.success(labApplyPurchaseService.selectLabApplyPurchaseById(instanceId));
	}
	/****
	 * 获取老师的申请内容列表.
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:teacher')")
	@GetMapping(value = "/teacher")
	public AjaxResult getTeacherAppy(Long purchaseId) {
		return AjaxResult.success(labApplyPurchaseService.selectForAcademyPurchase(purchaseId));
	}
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:academy')")
	@Log(title = "学院采购", businessType = BusinessType.UPDATE)
	@PostMapping("/academyUpdate")
	public AjaxResult academyUpdate(@RequestBody LabApplyPurchase labApplyPurchase) {
		labApplyPurchaseService.updateAcademyPurchase(labApplyPurchase);
		return AjaxResult.success(labApplyPurchase);
	}
	/**
	 * 新增申请采购
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:edit')")
	@Log(title = "申请采购", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabApplyPurchase labApplyPurchase) {
		labApplyPurchaseService.insertLabApplyPurchase(labApplyPurchase);
		return AjaxResult.success(labApplyPurchase);
	}

	/**
	 * 新增申请采购
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:edit')")
	@Log(title = "申请采购提交流程", businessType = BusinessType.INSERT)
	@PostMapping("/submit")
	public AjaxResult submit(@RequestBody LabApplyPurchase labApplyPurchase) {
		labApplyPurchaseService.submitLabApplyPurchase(labApplyPurchase);
		return AjaxResult.success(labApplyPurchase.getInstanceId());
	}

	/**
	 * 修改申请采购
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:edit')")
	@Log(title = "申请采购", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabApplyPurchase labApplyPurchase) {
		return toAjax(labApplyPurchaseService.updateLabApplyPurchase(labApplyPurchase));
	}

	/**
	 * 删除申请采购
	 */
	@PreAuthorize("@ss.hasPermi('labdanger:purchase:edit')")
	@Log(title = "申请采购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{instanceId}")
	public AjaxResult remove(@PathVariable Long instanceId) {
		return toAjax(labApplyPurchaseService.deleteLabApplyPurchaseById(instanceId));
	}
}
